package com.hedl.practice.diyJava.array;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Big Watermelon
 * @description
 * @date 2024/02/14/08:19
 */
public class ArrayUnion {

    /**
     * 求两个字符串数组的并集，利用set的元素唯一性
     * @param arr1
     * @param arr2
     * @return
     */
    public static String [] union(String []arr1 ,String []arr2){
        Set<String> set = new HashSet<>();

        if (arr1.length == 0 && arr2.length == 0){
            throw new RuntimeException("传入的元素为空！");
        }

        for (String s : arr1) {
            set.add(s);
        }

        for (String s : arr2) {
            set.add(s);
        }

        String [] result = {};
        return set.toArray(result);
    }

    public static void main(String[] args) {
        //String [] arr1 = {};
        //String [] arr2 = {};
        String [] arr1 = {"1","2","3","4","55","88","10"};
        String [] arr2 = {"3","2","5","10","33","66","1"};
        String[] result_union = union(arr1, arr2);
        System.out.println("并集的结果如下： ");

        for (String s : result_union) {
            System.out.println(s);
        }
    }
}
